<?php

// This function updates comments from users with the auto-hide
// 'permission', to mark them as deleted.  Auto-hide affects both
// new comments and comment edits.
function AutoHide_ScreenCommentForHide($ContextObject){
   // If the delegate parameter name is 'Comment', this is a new comment.
   // Otherwise, this is a comment edit.
   $Comment = '';
   $CommentContext = '';
   if (array_key_exists('Comment', $ContextObject->DelegateParameters)){
      // ContextObject is a CommentManager.
      $Comment = $ContextObject->DelegateParameters['Comment'];
   }
   elseif (array_key_exists('ResultComment',$ContextObject->DelegateParameters)){
      // ContextObject is a DiscussionForm.
      $Comment = $ContextObject->DelegateParameters['ResultComment'];
   }
   else{
      return;
   }
   $CommentContext = $ContextObject->Context;
   $CommentDatabase = $CommentContext->Database;
   if ($CommentContext->Session->User->Permission('PERMISSION_AUTO_HIDE')){
      // Tweak the comment instance in memory, in case it matters.
      $Comment->Deleted = '1';
      // Have to update the comment manually, because the Deleted field isn't inserted 
      // with new comments.
      $Query = $CommentContext->ObjectFactory->NewContextObject(
      	$CommentContext,"SqlBuilder");
      $Query->SetMainTable('Comment','c');
      $Query->AddFieldNameValue('Deleted','1');
      $Query->AddWhere('c','CommentID','',$Comment->CommentID,'=');
      $RowsUpdated = $CommentDatabase->Update($Query,'AutoHide','NewCommentDelegate',
      	$CommentContext->GetDefinition('AutoHide_ErrorHidingComment'));
      if ($RowsUpdated != 1){
         $CommentContext->WarningCollector->Add(
	         $CommentContext->GetDefinition('AutoHide_ErrorHidingComment'));
      }
   }
}

$Context->AddToDelegate('CommentManager','PostSaveNewComment',
	'AutoHide_ScreenCommentForHide');

$Context->AddToDelegate('DiscussionForm','PostSaveComment',
	'AutoHide_ScreenCommentForHide');

// This function updates new discussions from users with the auto-hide
// 'permission', to mark them as inactive.
function AutoHide_ScreenDiscussionForHide($DiscussionForm){
   $Discussion = $DiscussionForm->DelegateParameters['ResultDiscussion'];
   $DiscussionContext = $DiscussionForm->Context;
   $DiscussionDatabase = $DiscussionContext->Database;
   if ($DiscussionContext->Session->User->Permission('PERMISSION_AUTO_HIDE')){
      $Query = $DiscussionContext->ObjectFactory->NewContextObject(
	$DiscussionContext,"SqlBuilder");
      $Query->SetMainTable('Discussion','d');
      $Query->AddFieldNameValue('Active','0');
      $Query->AddWhere('d','DiscussionID','',$Discussion->DiscussionID,'=');
      $RowsUpdated = $DiscussionDatabase->Update($Query,'AutoHide','NewCommentDelegate',
	$DiscussionContext->GetDefinition('AutoHide_ErrorHidingDiscussion'));
      if ($RowsUpdated != 1){
         $DiscussionContext->WarningCollector->Add(
            $DiscussionContext->GetDefinition('AutoHide_ErrorHidingDiscussion'));
      }
      // We have to do the redirect ourselves, lest the user get redirected
      // to a discussion he can't view.
      // NOTE: This is a bad solution, but I'm not seeing an alternative.
      //   This extension probably should be last in the load list just in
      //   case the delegate we're attaching to is used by others.
      header('location:'.GetUrl($DiscussionContext->Configuration, ''));
      die();
   }
}

$Context->AddToDelegate('DiscussionForm','PostSaveDiscussion',
	'AutoHide_ScreenDiscussionForHide');

?>
